package cn.tedu.onlinestore.review.service.Impl;

import cn.tedu.onlinestore.review.mapper.ReviewMapper;
import cn.tedu.onlinestore.review.pojo.dto.AddReviewParam;
import cn.tedu.onlinestore.review.pojo.entity.Review;
import cn.tedu.onlinestore.review.pojo.vo.ReviewVO;
import cn.tedu.onlinestore.review.service.ReviewService;
import cn.tedu.onlinestore.user.mapper.UserMapper;
import cn.tedu.onlinestore.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ReviewServiceImpl implements ReviewService {
    @Autowired
    ReviewMapper reviewMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public void addReview(AddReviewParam addReviewParam) {
        log.debug("新增评论列表：{}",addReviewParam);
        Review review = new Review();
        BeanUtils.copyProperties(addReviewParam,review);
        review.setCreateTime(new Date());
        reviewMapper.insert(review);
    }

    @Override
    public List<ReviewVO> selectReviewById(Integer id) {
        log.debug("查询评论列表业务层：{}",id);
        List<ReviewVO> list  =  reviewMapper.selectReviewById(id);
        for (ReviewVO reviewVO : list) {
            Long userId = reviewVO.getUserId();
            UserVO userVO =userMapper.selectUserById(userId);
            reviewVO.setUsername(userVO.getUsername());
        }
        return list;
    }

    @Override
    public List<ReviewVO> getAllReview(Integer page, Integer size) {
        log.debug("查询所有评论业务层：{},{}",page,size);
        return reviewMapper.getAllReview(page,size);
    }

    @Override
    public void deleteReview(Integer id) {
        log.debug("删除评论业务层：{}",id);
        reviewMapper.deleteReview(id);
    }
}
